package com.rent.product.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rent.product.dto.PmsSpuInfoDto;
import com.rent.product.entity.PmsBrandEntity;
import com.rent.product.entity.PmsCategoryEntity;
import com.rent.product.entity.PmsSpuInfoEntity;
import com.rent.product.service.PmsBrandService;
import com.rent.product.service.PmsCategoryService;
import com.rent.product.service.PmsSpuInfoService;
import com.rent.product.utils.R;
import org.apache.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/spu/management")
public class SpuManagementController {

    @Resource
    private PmsSpuInfoService pmsSpuInfoService;
    @Resource
    private PmsBrandService pmsBrandService;
    @Resource
    private PmsCategoryService pmsCategoryService;

    /**
     * 根据查询条件返回符合条件的spu数据
     * @param page
     * @param limit
     * @param classificationId null *
     * @param brandId  null *
     * @param status null *
     * @param search_text null "" *
     * @return
     */
    @RequestMapping(value = "/giveshelveslists", method = RequestMethod.GET)
    public R giveShelvesLists(Integer page, Integer limit, Long classificationId, Long brandId, Integer status, String search_text){
        Boolean muchWrapper = Boolean.TRUE;
        QueryWrapper<PmsSpuInfoEntity> wrapper = new QueryWrapper<PmsSpuInfoEntity>();
        if(classificationId != null){
            muchWrapper = Boolean.FALSE;
            wrapper.eq("catalog_id",classificationId);
        }
        if(brandId != null){
            muchWrapper = Boolean.FALSE;
            wrapper.eq("brand_id",brandId);
        }
        if(status != null){
            muchWrapper = Boolean.FALSE;
            wrapper.eq("publish_status",status);
        }
        if(search_text != null && !search_text.equals("")){
            muchWrapper = Boolean.FALSE;
            wrapper.like("spu_description",search_text).or().like("spu_name",search_text);
        }
        List<PmsSpuInfoEntity> pmsSpuInfoEntityList;
        if(muchWrapper)
            pmsSpuInfoEntityList = pmsSpuInfoService.page(new Page<>(page,limit)).getRecords();
        else
            pmsSpuInfoEntityList = pmsSpuInfoService.page(new Page<>(page,limit), wrapper).getRecords();
        HashMap<String,Object> stringObjectHashMap = new HashMap<String,Object>();
        stringObjectHashMap.put("code",0);
        stringObjectHashMap.put("msg","");
        List<PmsSpuInfoDto> spuInfoDtoList = new ArrayList<PmsSpuInfoDto>();
        for(int i=0; i<pmsSpuInfoEntityList.size(); i++) {
            PmsSpuInfoDto spuInfoDto = new PmsSpuInfoDto();
            PmsSpuInfoEntity pmsSpuInfoEntity = pmsSpuInfoEntityList.get(i);
            spuInfoDto.setId(pmsSpuInfoEntity.getId());
            spuInfoDto.setSpuName(pmsSpuInfoEntity.getSpuName());
            spuInfoDto.setSpuDescription(pmsSpuInfoEntity.getSpuDescription());
            spuInfoDto.setWeight(pmsSpuInfoEntity.getWeight());
            spuInfoDto.setPublishStatus(pmsSpuInfoEntity.getPublishStatus());
            spuInfoDto.setCreateTime(pmsSpuInfoEntity.getCreateTime());
            spuInfoDto.setUpdateTime(pmsSpuInfoEntity.getUpdateTime());
            spuInfoDto.setBrand(pmsBrandService.getOne(new QueryWrapper<PmsBrandEntity>().like("brand_id",pmsSpuInfoEntity.getBrandId())).getName());
            spuInfoDto.setCatalog(pmsCategoryService.getCompleteCategory(pmsSpuInfoEntity.getCatalogId()));
            spuInfoDtoList.add(spuInfoDto);
        }
        stringObjectHashMap.put("data",spuInfoDtoList);
        if(muchWrapper)
            stringObjectHashMap.put("count",pmsSpuInfoService.count());
        else
            stringObjectHashMap.put("count",pmsSpuInfoService.count(wrapper));
        return R.ok(stringObjectHashMap);
    }

    /**
     * 更新spu的上下架状态
     * @param id
     * @param publishStatus
     * @return
     */
    @RequestMapping(value = "/updateshelvespublishstatus", method = RequestMethod.POST)
    public R updateShelveSpublishStatus(Long id, Integer publishStatus){
        Boolean updateEnd = pmsSpuInfoService.updateShelveSpublishStatus(id, publishStatus);
        if(updateEnd)
            return new R();
        else
            return R.error(HttpStatus.SC_INTERNAL_SERVER_ERROR,"记录异常");
    }

    /**
     * 获取分类数据
     * @return
     */
    @RequestMapping(value = "/getclassificationlists", method = RequestMethod.GET)
    public R getClassificationLists(){
        List<HashMap<String,Object>> classificationlists = pmsCategoryService.getClassificationLists();
        R r = new R();
        r.put("data",classificationlists);
        return r;
    }

}
